﻿using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Repository.Abstractions;
using Foundation.Uow;
using Foundation.Core;

namespace Dyao.Book.Application.Applications
{
    public class BookshelfApplication : BaseApplication, IBookshelfApplication
    {
        private readonly IBookshelfRepository _bookshelfRepository;

        public BookshelfApplication(IBookshelfRepository bookshelfRepository)
        {
            _bookshelfRepository = bookshelfRepository;
        }

        /// <inheritdoc cref="IBookshelfApplication.GetUserBookshelfAsync"/>
        public async Task<List<UserBookshelfDto>> GetUserBookshelfAsync(CancellationToken cancellationToken)
        {
            using (var uow = UnitOfWorkManager.Begin(isReadDb: true))
            {
                var bookRepo = GetService<IBookRepository>();
                var bookshelf = await _bookshelfRepository.GetListByUserIdAsync(CurrentUser.GetUserId(), bs => new
                {
                    bs.BookId,
                    bs.LastReadingChapterId,
                    bs.LastReadingDateTime
                }, cancellationToken);

                var bookIds = bookshelf.Select(b => b.BookId).ToList();

                var books = await bookRepo.GetListByIdsAsync(bookIds, b => new UserBookshelfDto()
                {
                    BookId = b.ID,
                    BookAuthor = b.Author,
                    BookDesc = b.Description,
                    BookName = b.Title,
                    BookPic = b.CoverPicture,
                    Category = b.Category,
                    SubCategory = b.SubCategory,
                }, cancellationToken);

                books.ForEach(b =>
                {
                    var currentBookshelf = bookshelf.FirstOrDefault(bs => bs.BookId == b.BookId);
                    b.LastReadingChapterId = currentBookshelf.LastReadingChapterId;
                    b.LastReadingDateTime = currentBookshelf.LastReadingDateTime;
                });

                return books;
            }
        }

        /// <inheritdoc cref="IBookshelfApplication.ModifyLastReadingChapterAsync"/>
        public async Task<bool> ModifyLastReadingChapterAsync(ModifyBookshelfLastReadingChapterDto dto, CancellationToken cancellationToken)
        {
            var chapterRepo = GetService<IBookCatalogueRepository>();
            using (var uow = UnitOfWorkManager.Begin())
            {
                var chapterEntity = await chapterRepo.GetByIdAsync(dto.ChapterId, cancellationToken);
                if (chapterEntity == null)
                    throw new BusinessException("书籍章节不存在");

                var book = await _bookshelfRepository.GetByUserIdAsync(CurrentUser.Id, dto.BookId, cancellationToken);
                if (book == null)
                    throw new BusinessException("不存在该书籍");

                book.LastReadingChapterId = dto.ChapterId;
                book.LastReadingDateTime = dto.LastReadingDateTime;

                await uow.CompleteAsync(cancellationToken);

                return true;
            }
        }
    }
}
